package com.taoyibao.hongcanting.ui.home.views;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.BaseRelativeLayout;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.model.DishModel;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.GlideUtil;

/**
 * @author TangAnna
 * @description: 购物车列表Item中的Item
 * @date :${DATA} 18:54
 */
public class SecondaryItemCartArea extends BaseRelativeLayout<DishModel> {

    /**
     * 菜品图片
     */
    private ImageView mIvPic;
    /**
     * 菜品名称
     */
    private TextView mTvName;
    /**
     * 菜品的口味和做法
     */
    private TextView mTvMakupTask;
    /**
     * 菜品优惠价格
     */
    private TextView mTvDiscountPrice;

    /**
     * 菜品数量 （只显示数量时使用）
     */
    private TextView mTvCount;

    /**
     * 显示数量控制器时使用
     */
    private CountControllerArea mCountControllerArea;


    public SecondaryItemCartArea(Context context) {
        super(context);
    }

    public SecondaryItemCartArea(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SecondaryItemCartArea(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        mCountControllerArea = findViewById(R.id.cca_sencondItemCart_countController);
        mCountControllerArea.setSource(CodeConstant.EVENT_SOURCE_CART);
        mTvCount = findViewById(R.id.tv_sencondItemCart_count);
        mTvDiscountPrice = findViewById(R.id.tv_sencondItemCart_discount_price);
        mTvName = findViewById(R.id.tv_sencondItemCart_name);
        mTvMakupTask = findViewById(R.id.tv_sencondItemCart_makup_taste);
        mIvPic = findViewById(R.id.iv_sencondItemCart_pic);
    }

    @Override
    public void setData(DishModel data) {
        Log.d("TAG", "setData: " + data);
        //菜品名称
        if (!TextUtils.isEmpty(data.dish_name)) {
            mTvName.setText(data.dish_name);
        }
        //菜品数量（只有数量）
        mTvCount.setText("x" + data.dish_count);
        //数量控制器
        mCountControllerArea.setData(data);
        //优惠价格
        if (data.dish_discount_price == -1) {//订单信息页面没有dish_discount_price使用dish_price值
            mTvDiscountPrice.setText(CodeUtils.formatAmountUnit(data.dish_price));
        } else {
            mTvDiscountPrice.setText(CodeUtils.formatAmountUnit(data.dish_discount_price));
        }
        //口味做法
        String makeupTaste = "";
        if (!TextUtils.isEmpty(data.makeup) && !TextUtils.equals("0", data.makeup)) {
            makeupTaste = data.makeup;
        }
        if (!TextUtils.isEmpty(data.taste) && !TextUtils.equals("0", data.taste)) {
            makeupTaste = makeupTaste + "    " + data.taste;
        }

        if (!TextUtils.isEmpty(makeupTaste)) {
            mTvMakupTask.setText(makeupTaste);
            mTvMakupTask.setVisibility(VISIBLE);
        } else if (!TextUtils.isEmpty(data.dish_practicetaste) && !TextUtils.equals(data.dish_practicetaste, "0,0")) {//订单信息口味和做法的参数
            mTvMakupTask.setText(data.dish_practicetaste);
            mTvMakupTask.setVisibility(VISIBLE);
        } else {
            mTvMakupTask.setVisibility(INVISIBLE);
        }

        //菜品图片
        GlideUtil.getInstance().loadDefaultImage(getContext(), data.pic_url, mIvPic);
    }

    /**
     * 设置是否显示数量控制器
     *
     * @param show true 显示（默认显示）  false 不显示
     */
    public void isShowCountController(boolean show) {
        //是否显示数量控制器
        if (show) {
            mCountControllerArea.setVisibility(VISIBLE);
            mTvCount.setVisibility(GONE);
        } else {
            mCountControllerArea.setVisibility(GONE);
            mTvCount.setVisibility(VISIBLE);
        }
    }

    @Override
    protected int bindLayout() {
        return R.layout.secondary_item_cart;
    }
}
